<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>礼物列表</title>
    #include("common/header.html")
    <link rel="stylesheet" href="/css/table.css"/>
</head>

<body>
<div class="admin-main">

    <blockquote class="layui-elem-quote">
        <a href="javascript:add();" class="layui-btn layui-btn-small">
            <i class="layui-icon">&#xe608;</i> 添加礼物
        </a>
    </blockquote>
    <fieldset class="layui-elem-field">
        <legend>礼物列表</legend>
        <div class="layui-field-box layui-form">
            <table class="layui-table admin-table">
                <thead>
                <tr>
                    <th>名称</th>
                    <th>图片</th>
                    <th>金额</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody id="pager">
                </tbody>
            </table>
        </div>
    </fieldset>
    <!--<div class="admin-table-page">-->
    <div class="page-table-box" id="page" style=" width:1000px; margin:0 auto; margin-top:100px;"></div>
    <!--</div>-->
</div>
</body>

<script type="text/javascript">

    $(function () {
        pageList(1);
    });

    function pageList(pageNum) {

        $.ajax({
            url: "/gift/page",
            type: "post",
            dataType: "json",
            data: {"pageNum": pageNum, "pageSize": window.page.pageSize},
            success: function (r) {

                console.log(r);

                if (r.code == -1) {
                    return;
                }

                page(pageNum, r.data.total);

                $("#pager").html("");

                var str = "";

                $.each(r.data.list, function (n, value) {
                    str += '<tr>';
                    str += '<td>' + value.name + '</td>';
                    var url = "/images/timg.jpg";
                    if(value.img != null){
                        url = value.img;
                    }
                    str += '<td><img src="'+url+'" width="30px;" height="30px;"></td>';
                    str += '<td>' + value.money + '</td>';
                    str += '<td>';
                    str += '<a onclick="del(' + value.id + ')" class="layui-btn layui-btn-danger layui-btn-mini">删除</a>';
                    str += '</td>';
                    str += '</tr>';
                });

                $("#pager").html(str);
            }
        });
    }


    var layer;
    layui.use('layer', function () {
        layer = layui.layer;
    });



    function del(id) {

        //询问框
        layer.confirm('确定删除?', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $.ajax({
                url: "/gift/"+id,
                type: "delete",
                dataType: "json",
                success: function (r) {
                    console.log(r);

                    if(r.code == -1){
                        layer.msg(r.msg);
                        return;
                    }

                    layer.msg('删除成功!',{time:1000}, function(){
                        location.reload();
                    });
                }
            });
        });
    }

    function add() {
        layer.open({
            type: 2,
            title: '添加礼物',
            shadeClose: true,
            shade: false,
            maxmin: true, //开启最大化最小化按钮
            area: ['650px', '450px'],
            content: '/gift/add',
            btn: ['确定', '取消'],
            yes: function (index, layero) {

                var body = layer.getChildFrame('body',index);
                var validate = body.find("#form").validator();

                if(!validate.check()){return;}

                var name = body.find("#name").val();
                var imgUrl = body.find("#imgUrl").attr("data-src");
                var money = body.find("#money").val();

                $.ajax({
                    url: "/gift/",
                    type: "post",
                    dataType: "json",
                    data: {
                        "name": name,
                        "img":imgUrl,
                        "money":money
                    },
                    success: function (r) {
                        console.log(r);

                        if (r.code == -1) {
                            layer.msg(r.msg);
                            return;
                        }

                        layer.close(index);
                        layer.msg('添加成功!', {time: 1000}, function () {
                            location.reload();
                        });
                    }
                });

            }
        });
    }

    function info(id) {
        layer.open({
            type: 2,
            title: '详情',
            shadeClose: true,
            shade: false,
            maxmin: true, //开启最大化最小化按钮
            area: ['650px', '450px'],
            content: '/admin/vedio/'+id,
        });
    }

    function edit(id) {
        layer.open({
            type: 2,
            title: '添加视频',
            shadeClose: true,
            shade: false,
            maxmin: true, //开启最大化最小化按钮
            area: ['650px', '450px'],
            content: '/admin/vedio/edit/'+id,
            btn: ['确定', '取消'],
            yes: function (index, layero) {

                var body = layer.getChildFrame('body',index);
                var validate = body.find("#form").validator();

                if(!validate.check()){return;}

                var title = body.find("#title").val();
                var type = body.find("#type").val();
                var imgUrl = body.find("#imgUrl").attr("data-src");
                var url = body.find("#url").val();
                var isMoney = body.find("#isMoney").val();
                var money = body.find("#money").val();

                $.ajax({
                    url: "/admin/vedio/",
                    type: "put",
                    dataType: "json",
                    data: {
                        "id": id,
                        "title": title,
                        "imgUrl":imgUrl,
                        "type":type,
                        "url":url,
                        "isMoney":isMoney,
                        "money":money
                    },
                    success: function (r) {
                        console.log(r);

                        if (r.code == -1) {
                            layer.msg(r.msg);
                            return;
                        }

                        layer.close(index);
                        layer.msg('修改成功!', {time: 1000}, function () {
                            location.reload();
                        });
                    }
                });

            }
        });
    }

</script>

</html>